# -*- mode: python; -*-
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
import platform
import os

def MapBuildDir(list):
    return map(lambda x: '#/' + Dir('.').path + '/../' + x, list)

env = BuildEnv.Clone()

libcontrol_node = env.Library('control_node', ['control_node.cc'])

env.Append(CPPPATH = [env['TOP'] + '/base/sandesh'])
env.Append(CPPPATH = [env['TOP'] + '/db'])
env.Append(CPPPATH = [env['TOP'] + '/ifmap'])
env.Append(CPPPATH = [env['TOP'] + '/config-client-mgr'])
env.Append(CPPPATH = [env['TOP'] + '/bgp'])
env.Append(CPPPATH = [env['TOP'] + '/tools/sandesh/library/common'])
ControlNodeSandeshGenFiles = env.SandeshGenCpp('sandesh/control_node.sandesh')
ControlNodeSandeshGenFiles.append("control_node_uve.cc")
ControlNodeSandeshGenSrcs = env.ExtractCpp(ControlNodeSandeshGenFiles)

setup_sources = [
           'setup.py',
           'MANIFEST.in',
          ]

setup_sources_rules = []
for file in setup_sources:
    setup_sources_rules.append(env.Install(Dir("."), "#controller/src/control-node/sandesh/" + file))

# Generate the sandesh UVE source files
control_node_pkg  = env.SandeshGenPy('sandesh/control_node.sandesh', 'control_node/', False)
cpuinfo_pkg = env.SandeshGenPy('#src/contrail-common/base/sandesh/cpuinfo.sandesh', 'control_node/control_node/', False)
process_info_pkg = env.SandeshGenPy('#src/contrail-common/base/sandesh/process_info.sandesh', 'control_node/control_node/', False)
ifmap_pkg = env.SandeshGenPy('#controller/src/ifmap/ifmap_server_show.sandesh', 'control_node/control_node/', False)
env.Depends(ifmap_pkg, control_node_pkg)
env.Depends(cpuinfo_pkg, control_node_pkg)
env.Depends(process_info_pkg, control_node_pkg)

rel_path = Dir('control_node').path
def BuildInfoAction(target, source, env):
    env.GenerateBuildInfoPyCode(path=rel_path)
    
build_info_rules = [env.Command(target='control_node/buildinfo.py', source = None, action=BuildInfoAction)]

cd_cmd = 'cd ' + Dir('.').path + ' && '
sdist_depends = []
sdist_depends.extend(setup_sources_rules)
sdist_depends.extend(control_node_pkg)
sdist_depends.extend(ifmap_pkg)
sdist_depends.extend(cpuinfo_pkg)
sdist_depends.extend(process_info_pkg)
sdist_depends.extend(build_info_rules)
sdist_gen = env.Command('dist', File('setup.py'), cd_cmd + 'python setup.py sdist')

env.Depends(sdist_gen, sdist_depends)
env.Default(sdist_gen)
env.Alias("control-node:node_mgr", sdist_gen)

libcontrail_control_uve = env.Library('control_node_uve',
                                      ControlNodeSandeshGenSrcs);

etcd_incl = os.environ.get('CONTRAIL_ETCD_INCL');

if etcd_incl:
    env.Prepend(LIBS=[
                  'etcdql',
                  'protobuf',
                  'grpc++',
                  'grpc', 'z'])

env.Prepend(LIBS=['bgp',
                  'bgp_ifmap_config',
                  'bgp_schema',
                  'bgp_xmpp',
                  'extended_community',
                  'xmpp_unicast',
                  'xmpp_multicast',
                  'xmpp_mvpn',
                  'xmpp_enet',
                  'control_node',
                  'control_node_uve',
                  'peer_sandesh',
                  'origin_vn',
                  'routing_instance',
                  'routing_policy',
                  'rtarget',
                  'security_group',
                  'tunnel_encap',
                  'ifmap_vnc',
                  'ifmap_server',
                  'ifmap_common',
                  'route',
                  'net',
                  'config_client_mgr',
                  'ifmapio',
                  'xmpp',
                  'xml',
                  'sandeshvns',
                  'sandesh',
                  'httpc',
                  'http',
                  'http_parser',
                  'curl',
                  'db',
                  'process_info',
                  'io',
                  'base',
                  'cassandra_cql',
                  'SimpleAmqpClient',
                  'rabbitmq',
                  'cassandra',
                  'gendb',
                  'cpuinfo',
                  'process_info',
                  'nodeinfo',
                  'pugixml',
                  'libxml2',
                  'boost_regex',
                  'boost_chrono',
                  'boost_program_options',
                  'boost_filesystem',
                  'crypto', 'ssl'])

if platform.system() != 'Darwin':
    env.Append(LIBS=['rt'])

libs = MapBuildDir(['base',
                    'bgp',
                    'bgp/inet',
                    'bgp/inet6',
                    'bgp/inet6vpn',
                    'bgp/ermvpn',
                    'bgp/mvpn',
                    'bgp/evpn',
                    'bgp/extended-community',
                    'bgp/l3vpn',
                    'bgp/origin-vn',
                    'bgp/routing-instance',
                    'bgp/routing-policy',
                    'bgp/rtarget',
                    'bgp/security_group',
                    'bgp/tunnel_encap',
                    'control-node',
                    'db',
                    'ifmap',
                    'io', 
                    'net',
                    'route',
                    'xmpp',
                    'xml'])

env.Append(LIBPATH=libs)
env.Append(LIBPATH = env['TOP'] + '/schema')
env.Append(CPPPATH = env['TOP'])
env.Append(CPPPATH = env['TOP'] + '/control-node')
env.Append(CPPPATH = env['TOP'] + '/io')

lib_l3vpn = File('../bgp/l3vpn/libbgp_l3vpn.a')
lib_inet = File('../bgp/inet/libbgp_inet.a')
lib_inet6 = File('../bgp/inet6/libbgp_inet6.a')
lib_inet6vpn = File('../bgp/inet6vpn/libbgp_inet6vpn.a')
lib_ermvpn = File('../bgp/ermvpn/libbgp_ermvpn.a')
lib_mvpn = File('../bgp/mvpn/libbgp_mvpn.a')
lib_evpn = File('../bgp/evpn/libbgp_evpn.a')
lib_rtarget = File('../bgp/rtarget/librtarget.a')
lib_ifmap_server = File('../ifmap/libifmap_server.a')
lib_sandesh = File('../sandesh/library/cpp/libsandesh.a')
lib_cpuinfo = File('../base/libcpuinfo.a')
lib_process_info = File('../base/libprocess_info.a')
lib_nodeinfo = File('../base/libnodeinfo.a')
lib_base = File('../base/libbase.a')
lib_extend_community = File('../bgp/extended-community/libextended_community.a')

if platform.system() != 'Darwin':
    env.Prepend(LINKFLAGS =
                     ['-Wl,--whole-archive',
                      '-lbgp_l3vpn', '-lbgp_inet',
                      '-lbgp_inet6', '-lbgp_inet6vpn',
                      '-lbgp_evpn', '-lbgp_ermvpn', '-lextended_community', '-lrtarget',
                      '-lifmap_server', '-lcpuinfo', '-lprocess_info', '-lnodeinfo',
                      '-lrouting_instance', '-lrouting_policy', '-lbgp_mvpn',
                      '-lbase', '-Wl,--no-whole-archive'])
else:
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_l3vpn.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_inet.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_inet6.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_inet6vpn.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_ermvpn.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_mvpn.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_evpn.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_rtarget.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_ifmap_server.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_sandesh.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_cpuinfo.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_process_info.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_nodeinfo.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_base.path])
    env.Prepend(LINKFLAGS = ['-Wl,-force_load,' + lib_extend_community.path])

buildinfo_dep_libs = ['../bgp/libbgp.a', 
                      '../bgp/libbgp_ifmap_config.a',
                      '../schema/libbgp_schema.a', '../schema/libxmpp_unicast.a', 
                      '../schema/libxmpp_multicast.a', '../schema/libxmpp_enet.a',
                      '../schema/libxmpp_mvpn.a',
                      '../control-node/libcontrol_node.a',
                      '../control-node/libcontrol_node_uve.a',
                      '../bgp/routing-instance/librouting_instance.a',
                      '../bgp/routing-policy/librouting_policy.a',
                      '../bgp/origin-vn/liborigin_vn.a',
                      '../bgp/extended-community/libextended_community.a',
                      '../bgp/rtarget/librtarget.a', '../bgp/security_group/libsecurity_group.a', '../schema/libifmap_vnc.a', 
                      '../ifmap/libifmap_server.a', '../ifmap/libifmap_common.a', '../route/libroute.a', '../net/libnet.a', 
                      '../../lib/libifmapio.a', '../xmpp/libxmpp.a', '../xml/libxml.a', '../../lib/libsandeshvns.a', 
                      '../../lib/libsandesh.a', '../../lib/libhttp.a', '../../lib/libhttp_parser.a',
                      '../db/libdb.a', '../io/libio.a', '../base/libbase.a', '../base/libcpuinfo.a', '../base/libprocess_info.a',
                      '../base/libnodeinfo.a', '../../lib/libpugixml.a',
		      ]

env.GenerateBuildInfoCode(
	target = ['buildinfo.h', 'buildinfo.cc'],
	source = ControlNodeSandeshGenSrcs + buildinfo_dep_libs +
		['main.cc', 'options.cc'],
	path = Dir('.').path)

ctrlnode = env.Program(target = 'contrail-control',
                       source = ['buildinfo.cc',
                                 'factory.cc',
                                 'main.cc',
                                 'options.cc'])

env.Default(ctrlnode)

env.Depends(ctrlnode, lib_l3vpn)
env.Depends(ctrlnode, lib_inet)
env.Depends(ctrlnode, lib_inet6)
env.Depends(ctrlnode, lib_inet6vpn)
env.Depends(ctrlnode, lib_ermvpn)
env.Depends(ctrlnode, lib_mvpn)
env.Depends(ctrlnode, lib_evpn)
env.Depends(ctrlnode, lib_rtarget)
env.Depends(ctrlnode, lib_ifmap_server)

env.Alias('control-node', ctrlnode)
env.Alias('contrail-control', ctrlnode)

env.SConscript('test/SConscript', exports='BuildEnv', duplicate = 0)

#
# contrail-control message documentation
#
doc_files = []
doc_files += env.SandeshGenDoc('sandesh/control_node.sandesh')
doc_files += env['BGP_DOC_FILES']
doc_files += env['BASE_DOC_FILES']
doc_files += env['IO_DOC_FILES']
doc_files += env['DB_DOC_FILES']
doc_files += env['XMPP_DOC_FILES']
doc_files += env['IFMAP_SERVER_DOC_FILES']
doc_files += env['SANDESH_DOC_FILES']

# Please update sandesh/common/vns.sandesh on process name change
env.Alias('install', env.Install(env['INSTALL_BIN'], ctrlnode))
env.Alias('install', env.Install(env['INSTALL_CONF'], 
    'contrail-control.conf'))
env.Alias('install', env.Install(env['INSTALL_CONF'], 
    'supervisord_control.conf'))
env.Alias('install', env.Install(env['INSTALL_CONF'] + 
    '/supervisord_control_files', 'contrail-control.ini'))
env.Alias('install', env.Install(env['INSTALL_CONF'] + 
    '/supervisord_control_files', 'contrail-control.rules'))
env.Alias('install', env.InstallAs(env['INSTALL_INITD'] + 
    '/contrail-control', 'contrail-control.initd.supervisord'))
env.Alias('install', env.InstallAs(env['INSTALL_SYSTEMD'] +
    '/contrail-control.service', 'systemd/contrail-control.service'))
env.Alias('install', env.Install(env['INSTALL_MESSAGE_DOC'] +
    '/contrail-control/', doc_files))
